public class InterviewQuestion17_10 {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};

        System.out.println(majorityElement(nums));
    }

    public static int majorityElement(int[] nums) {
        int candidate = -1, count = 0;
        for (int num : nums) {
            if (count == 0) {
                candidate = num;
            }
            if (candidate == num) {
                count++;
            } else {
                count--;
            }
        }
        count = 0;
        for (int num : nums) {
            if (candidate == num) {
                count++;
            }
        }
        return count * 2 > nums.length ? candidate : -1;
    }

}
